package com.boc.ewr.bcimp.service.impl;

import com.boc.ewr.bcimp.entity.CaBalDtl;
import com.boc.ewr.bcimp.entity.CaUnpriaccList;
import com.boc.ewr.bcimp.mapper.CaBalDtlMapper;
import com.boc.ewr.bcimp.service.ICaBalDtlService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 余额对账单明细
 * @Author: jeecg-boot
 * @Date:   2023-04-20
 * @Version: V1.0
 */
@Slf4j
@Service
public class CaBalDtlServiceImpl extends ServiceImpl<CaBalDtlMapper, CaBalDtl> implements ICaBalDtlService {

    @Autowired
    CaBalDtlMapper caBalDtlMapper;

    //批量导入
    @Transactional
    public boolean initSaveBatch(Collection<CaBalDtl> entityList, Boolean flag){
        if(entityList.isEmpty() || entityList.size() == 0){
            return false;
        }
        long start = System.currentTimeMillis();
        //以最后一次导入为准，不变更不替代原数据
        List<CaBalDtl> caUnpriaccList = list();
        entityList.addAll(caUnpriaccList);

        entityList = entityList.stream()
                .collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(
                            Comparator.comparing(CaBalDtl::getCaNo ))), ArrayList::new));//去重，账户号/文书合同号

        log.info("数据整理完成, 所耗时间:" + (System.currentTimeMillis() - start) + "ms");
        start = System.currentTimeMillis();
        //整理好数据后，清空所有记录
        if (flag) {
            caBalDtlMapper.delete(null);
            log.info("清空余额对账单明细数据, 所耗时间:" + (System.currentTimeMillis() - start) + "ms");
        }

        // 再统一插入数据
        boolean res = saveBatch(entityList);
        return res;
    }

}
